package org.mindrive.system.web.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mindrive.system.annotation.LogOperation;
import org.mindrive.system.core.Result;
import org.mindrive.system.entity.OptionEntity;
import org.mindrive.system.parameter.system.SearchLogParameter;
import org.mindrive.system.service.BaseService;
import org.mindrive.system.service.system.LogService;
import org.mindrive.system.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = {"日志管理"})
@RestController
@RequestMapping("/log")
@Slf4j
public class LogController extends BaseController {

    @Autowired
    private LogService logService;

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "操作日志查询")
    @ApiOperation(value = "操作日志查询", notes = "操作日志查询")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/operations")
    public Result searchAuditOperations(@Validated @RequestBody SearchLogParameter parameter,
            BindingResult bindingResult) {

        Result result = new Result();
        try {
            validParams(bindingResult);
            Map<String, Object> data = logService.searchAuditOperations(parameter);
            result = Result.ok(data);
        } catch (Exception ex) {

            log.error("操作日志查询失败。", ex);
            result = Result.error(ex.getMessage());
        }

        return result;
    }

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "查询日志事件类型")
    @ApiOperation(value = "取得日志事件类型", notes = "取得日志事件类型")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @GetMapping(path = "/event-types")
    public Result getAuditEventTypes() {
        log.info("取得日志事件类型开始。");
        Result result = new Result();
        try {
            List<OptionEntity> list = logService.getAuditEventTypes();
            result = Result.ok(list);
        } catch (Exception ex) {

            log.error("取得日志事件类型失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("取得日志事件类型结束。");
        return result;
    }

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "操作日志查询详细")
    @ApiOperation(value = "操作日志查询详细", notes = "操作日志查询详细")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @GetMapping(path = "/{id}")
    public Result detail(@PathVariable(value = "id") String id) {
        log.info("操作日志查询详细开始。");
        Result result = new Result();
        try {
            Map<String, Object> data = logService.detail(id);
            result = Result.ok(data);
        } catch (Exception ex) {
            log.error("操作日志查询详细失败。", ex);
            result = Result.error(ex.getMessage());
        }
        return result;
    }


}
